package com.kb5173.search.words.service.impl;

import com.google.common.base.Joiner;
import com.kb5173.framework.common.AbstractBusinessObject;
import com.kb5173.framework.logging.enums.LogCategory;
import com.kb5173.search.words.service.IFileService;
import com.kb5173.search.words.service.IWordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;

/**
 * @author Chengfei.Sun
 */
@Component
@Lazy(false)
public class FileServiceImpl extends AbstractBusinessObject implements IFileService {
    @Value("${participle.file.path}")
    private String participleFilePath = "C:/Users/ThinkPad/Desktop/快捷方式/ext.dic";

    @Autowired
    IWordsService wordsManager;

    @Override
    public void wordsWrite(List<String> words) {
        if (words == null || words.size() == 0) {
            return;
        }

        // 组装文件写入内容
        Joiner joiner = Joiner.on("\r\n");
        String fileStr = "\r\n" + joiner.join(words);

        try {
            logger.debug("开始写入文件：{}", participleFilePath);
            OutputStreamWriter outputStream =
                    new OutputStreamWriter(new FileOutputStream(participleFilePath, true), "UTF-8");
            outputStream.write(fileStr);
            outputStream.close();
            logger.debug(LogCategory.SEARCH, "结束写入文件");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
